﻿using System;
using System.Collections.Generic;
using System.Data;
using DevTeam;

namespace ConfExpert.Forms.ImportWizards
{
    public partial class Destex_GF : XITopCekiListesi
    {
        int YuklemeId;
        public Destex_GF(int YuklemeId) :
            base(YuklemeId)
        {
            InitializeComponent();

            this.YuklemeId = YuklemeId;
            Init("Destex GF Çeki Listesi", true);
            this.FileDone += this_FileDone;
            //this.SplitterWidthPercentage = 30;
        }

        void this_FileDone(DataSet ds, string FileName)
        {
            foreach (DataTable dt in ds.Tables)
            {
                string article = dt.TableName;
                if (article == "general qty")
                    continue;

                if (Article.Select("Article='" + article + "'").Length == 0)
                {
                    DataRow drArticle = Article.NewRow();
                    drArticle["Article"] = article;
                    Article.Rows.Add(drArticle);
                }

                int basliksatiri = 0;
                for (int i = 0; i < dt.Rows.Count; i++)
                    if (dt.Rows[i][0].ToString() == "ROLL NO")
                    {
                        basliksatiri = i;
                        break;
                    }

                for (int grup = 0; grup < 4; grup++)
                {
                    Dictionary<string, int> indexes = new Dictionary<string, int>();
                    for (int colIndex = 0; colIndex < 3; colIndex++)
                    {
                        DataRow dr0 = dt.Rows[basliksatiri];
                        switch (dr0[colIndex].ToString())
                        {
                            case "SHSDE NO": indexes.Add("LotNo", grup * 3 + colIndex); break;
                            case "ROLL NO": indexes.Add("VendorRollId", grup * 3 + colIndex); break;
                            case "QTY": indexes.Add("Qty", grup * 3 + colIndex); break;
                        }
                    }

                    for (int i = basliksatiri + 1; i < dt.Rows.Count; i++)
                    {
                        DataRow dr = dt.Rows[i];

                        if (dr[indexes["Qty"]] != DBNull.Value)
                            if (dr[indexes["VendorRollId"]].ToStr() != "TOTAL")
                            {
                                DataRow drRoll = Roll.NewRow();
                                drRoll["Article"] = article;
                                drRoll["VendorRollId"] = dr[indexes["VendorRollId"]];
                                drRoll["Qty"] = dr[indexes["Qty"]];
                                drRoll["LotNo"] = dr[indexes["LotNo"]];

                                Roll.Rows.Add(drRoll);
                            }
                    }
                }
            }

        }

        
    }
}


